<?php
/**
 * compoundspectrum
 * 分析谱图
 * @author Administrator
 * @version
 */
require_once ROOT_PATH . '/library/Common/Classes/Upload.php';
require_once ROOT_PATH . '/library/Common/Classes/ImgUtil.php';
class Model_Tables_Compoundspectrum extends Zend_Db_Table_Abstract {
    /**
     * The default table name
     */
    protected $_name = 'compoundspectrum';
    /**
     * add
     * 增加分析谱图
     * @param $params 字段信息
     */
    public function add( $params )
    {
        $data = array(
		'compoundid' => $params['compoundid'],
        'compoundsaveid' => $params['compoundsaveid'],
		'spectrumtypeid' => $params['spectrumtypeid'],
        //'spectrumname' => $params['spectrumname'],
        'spectrumdes' => $params['spectrumdes'],
		//'spectrumurl' => $params['spectrumurl'],
		'orderid' => $params['orderid'],
        );
        $upload = new Upload();
        $imgUtil = new ImgUtil();
        $spectrumfiles = $upload->uploadMultiFile("spectrum","/tmp",false,false);
        if(count($spectrumfiles)>0 && $spectrumfiles[0]["to"]!=""){
        	$data["spectrumurl"] = $spectrumfiles[0]["to"];
        	$data["spectrumname"] = $spectrumfiles[0]["from"];
        	$path =  APPLICATION_PATH . '/../public/upload/file/' . $data["spectrumurl"];
        	$imgUtil->scale($path, 650, 3000);
        }
        //print_r($spectrumfiles);exit;
        return $this->insert($data);
    }
    /**
     * modify
     * 编辑分析谱图
     * @param $params 字段信息
     * @param $id 主键值
     */
    public function modify( $params, $id )
    {
        $data = array(
		'compoundid' => $params['compoundid'],
        'compoundsaveid' => $params['compoundsaveid'],
		'spectrumtypeid' => $params['spectrumtypeid'],
        //'spectrumname' => $params['spectrumname'],
        'spectrumdes' => $params['spectrumdes'],
		//'spectrumurl' => $params['spectrumurl'],
		'orderid' => $params['orderid'],
        );
        $upload = new Upload();
        $imgUtil = new ImgUtil();
        $spectrumfiles = $upload->uploadMultiFile("spectrum","/tmp",false,false);
        if(count($spectrumfiles)>0){
        	if($spectrumfiles[0]["to"]!=""){
        		$data["spectrumurl"] = $spectrumfiles[0]["to"];
        		$data["spectrumname"] = $spectrumfiles[0]["from"];
        		$path =  APPLICATION_PATH . '/../public/upload/file/' . $data["spectrumurl"];
        		$imgUtil->scale($path, 650, 3000);
        	}        	
        }
        //print_r($spectrumfiles);exit;
        $where = 'id = '.$id ;
        return $this->update($data , $where );
    }
    /**
     * modifyAjax
     * 无刷新编辑分析谱图
     * @param $id 主键值
     * @param $field 字段
     * @param $value 值
     */
    public function modifyAjax( $id, $field, $value )
    {
    	$db = $this->getAdapter();
    	$data = array($field => $value);
    	$where = $db->quoteInto("id=?", $id);
        return $this->update($data , $where );
    }
    /**
     * modifyAjaxRevers
     * 无刷新字段反转分析谱图
     * @param $field 需要反转的字段
     * @param $id 主键值
     */
    public function modifyAjaxRevers( $field ,$pri)
    {
    	$db = $this->getAdapter();
    	$sql = "update compoundspectrum set  $field =not  $field  where id=' $pri'";
		return $db->query($sql);
    }
    /**
     * del
     * 根据条件删除分析谱图
     * @param $id 主键值
     */
    public function del( $id )
    {
     	$where = 'id = '.$id;
        return $this->delete($where);
    }
    /**
     * getInfo
     * 获取分析谱图信息
     * @param $id 主键值
     * @return 结果数组
     */
    public function getInfo($id)
    {
        $row = $this->fetchRow('id = ' . $id);
        if (!$row) {
        	return null;
        }
        else
        {
        	return $row->toArray();
        }
    }
    /**
     * getCount
     * 获取分析谱图数量
     * @param int  搜索条件
     * @return 结果数
     */
    public function getCount($s = "",$compoundid=-1)
    { 
    	$db = $this->getAdapter();
    	$compoundid = intval($compoundid);
    	$where = $db->quoteInto("compoundid=?", $compoundid);
        $select = $this->select()->from($this->_name,'count(*) as num');
    	if($s!="")  
    	{
			$where .= $db->quoteInto(" and id=?", $s);    		
    	}
    	$select = $select->where($where);
        $result = $this->fetchRow($select)->num;
        return $result;
    }
    /**
     * getList
     * 取分析谱图数据分页
     * @param int  页码
     * @param int  每页的行数 
     * @param  搜索条件
     * @return 行数组
     */
    public function getList($page, $numPerPage = 10,$s= "",$compoundid=-1)
    {
    	$db = $this->getAdapter();
    	$compoundid = intval($compoundid);
    	$where = $db->quoteInto("compoundid=?", $compoundid);
    	$page = (1>$page) ? 1 : $page;
    	$offset = ($page-1)*$numPerPage;
        $orderby = array('id');
        if($s!="")     
		{
			$where .= $db->quoteInto(" and id= ?", $s);
		}  
        $result = $this->fetchAll($where,$orderby, $numPerPage, $offset )->toArray();
        return $result;
    }
    public function getIdsbycompoundsaveid($compoundsaveid){
    	$db = $this->getAdapter();
    	$sql = "select spectrumurl, orderid from compoundspectrum where compoundsaveid =:compoundsaveid and spectrumurl is not null";
    	return $db->fetchAll($sql,array("compoundsaveid"=>$compoundsaveid));
    }
}